// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')
const del = require('del')
const browserSync = require('browser-sync');
const loadPlugins = require('gulp-load-plugins');
const plugins = loadPlugins();
const bs = browserSync.create();

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

const style = () => {
  // base 参数指的是转换后的基准路径
  // outputStyle: 'expanded',指css样式全部展开
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: 'expanded' }))
    .pipe(dest('temp'))
}

const script = () => {
  // 要单独去安装 @babel/core  
  // @babel/preset-env 默认会把ES6的新特性全部转换
  // presets一定要传，不然会转换无效，babel只是一个平台，转换依靠的是babel的插件
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
}

const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data: data, defaults: { cache: false } }))
    .pipe(dest('temp'))
}

const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('dist'))
}

const useref = () => {
  // 合并temp目类下的html文件里引入的css,js，并进行压缩
  return src('temp/*.html', { base: 'temp' })
    // searchPath指去哪里路径下找这些文件
    // if插件判断指定的文件类型压缩
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(
      plugins.if(/\.html$/,plugins.htmlmin({ 
          // 默认只压缩空白字符
          collapseWhitespace: true,
          minifyCSS: true,
          minifyJS: true
        })
      )
    )
    .pipe(dest('dist'))
}

// 服务任务，监听文件变化和开启一个服务器
const serve = () => {
  watch('src/assets/styles/*.scss', style);
  watch('src/assets/scripts/*.js', script);
  watch('src/*.html', page);
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload);

  bs.init({
    notify: false, // 提示
    port: 8000, // 端口
    open: true, // 自动打开页面
    files: 'temp/**', // 启动后自动监听的文件
    server: { 
      // 文件寻找路径顺序
      baseDir: ['temp', 'src', 'public'],
      // 配置了这个会优先于baseDir
      routes: { 
        '/node_modules': 'node_modules'
      }
    }
  });
}

const clean = () => {
  return del(['dist', 'temp'])
}

const compile = parallel(style, script, page);

// 构建任务
const build = series(
  clean,
  parallel(series(compile, useref), image, font, extra)
);

// 开发任务
const develop = series(compile, serve);

module.exports = {
  clean,
  build,
  develop
}
